﻿using FastGPT_Admin.Service;
using FastGPT_Admin.Setting;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;

namespace FastGPT_Admin.ViewModel
{
    internal class ShowDataViewModel : ViewModel
    {
        private string connectionString = AppSettings.GlobalSettingsInstance.ConnectionString;
        private string databaseName = "fastgpt";
        // private string collectionName = "";

        // 用户数据源
        ObservableCollection<Users> _usersList = new ObservableCollection<Users>();
        public ObservableCollection<Users> usersList
        {

            get { return _usersList; }
            set
            {
                if (_usersList != value)
                {
                    _usersList = value;
                    RaisePropertyChanged(nameof(usersList));
                }
            }
        }
        private UsersService _usersService;

        // 团队数据源
        ObservableCollection<Teams> _teamsList = new ObservableCollection<Teams>();
        public ObservableCollection<Teams> teamsList
        {

            get { return _teamsList; }
            set
            {
                _teamsList = value;
                RaisePropertyChanged("teamsList");
            }
        }
        private TeamsService _teamsService;

        // 团队用户数据源
        ObservableCollection<TeamMembers> _teamMembersList = new ObservableCollection<TeamMembers>();
        public ObservableCollection<TeamMembers> teamMembersList
        {

            get { return _teamMembersList; }
            set
            {
                _teamMembersList = value;
                RaisePropertyChanged("teamMembersList");
            }
        }
        private TeamMembersService _teamMembersService;

        // 构造函数
        public ShowDataViewModel()
        {
            if (!string.IsNullOrEmpty(connectionString))
            {
                LoadUsers();
                LoadTeams();
                LoadTeamMembers();
            }
        }

        // 刷新数据
        public void RefreshData(string searchUsers = "", string searchTeams = "")
        {
            // 清空现有数据
            usersList.Clear();
            teamsList.Clear();
            teamMembersList.Clear();

            if (!string.IsNullOrEmpty(connectionString))
            {
                // 重新加载数据
                LoadUsers(searchUsers);
                LoadTeams(searchTeams);
                LoadTeamMembers();
            }
        }

        private void LoadUsers(string search = "")
        {
            string collectionName = "users";
            _usersService = new UsersService(connectionString, databaseName, collectionName);
            List<Users> readUsersArray = _usersService.Read(search);

            foreach (Users item in readUsersArray)
            {
                usersList.Add(new Users()
                {
                    Id = item.Id,
                    Username = item.Username,
                    Password = item.Password,
                    Status = item.Status,
                    Avatar = item.Avatar,
                    PromotionRate = item.PromotionRate,
                    Timezone = item.Timezone,
                    CreateTime = item.CreateTime,
                    Version = item.Version
                });
            }
        }

        private void LoadTeams(string searchTeams = "")
        {
            string collectionName = "teams";
            _teamsService = new TeamsService(connectionString, databaseName, collectionName);
            List<Teams> readTeamsArray = _teamsService.Read(searchTeams);

            foreach (Teams item in readTeamsArray)
            {
                teamsList.Add(new Teams()
                {
                    Id = item.Id,
                    Name = item.Name,
                    OwnerId = item.OwnerId,
                    Avatar = item.Avatar,
                    CreateTime = item.CreateTime,
                    Version = item.Version
                });
            }
        }

        private void LoadTeamMembers()
        {
            string collectionName = "team_members";
            _teamMembersService = new TeamMembersService(connectionString, databaseName, collectionName);
            List<TeamMembers> readTeamMembersArray = _teamMembersService.Read();

            foreach (TeamMembers item in readTeamMembersArray)
            {
                teamMembersList.Add(new TeamMembers()
                {
                    Id = item.Id,
                    TeamId = item.TeamId,
                    UserId = item.UserId,
                    Name = item.Name,
                    Role = item.Role,
                    Status = item.Status,
                    CreateTime = item.CreateTime,
                    DefaultTeam = item.DefaultTeam,
                    Avatar = item.Avatar,
                    Version = item.Version
                });
            }
        }
    }
}
